layui.define([ 'form', 'laydate', 'table', ], function(exports) {
	var form = layui.form;
	var table = layui.table;
    var index = layui.index;
    
   var view = {
       init:function(){
    	this.aliyunVerifyCode();
    	this.initSubmit();
        
        
        },
        
       aliyunVerifyCode:function(){ //阿里云验证码验证
      	 var wait=60;
           var state = true;

           function time() {
               if (wait == 0) {
                   state = true;
                   $('.huoqu_vcode').text("免费获取");
                   wait = 60;
                   $('.huoqu_vcode').attr("disabled", false);
               } else {
                   $('.huoqu_vcode').attr("disabled", true);
                   $('.huoqu_vcode').text(wait + "秒后可重发");
                   wait--;
                   setTimeout(function() {
                       time()
                   },1000)
               }
           }

           
           $(document).on('click','.huoqu_vcode',function(){
          	 if(!aliVerifyStatus){
              	 layer.alert("请点击验证按钮", {icon: 2});
              	 return false;
               }
          	 
               if($(this).attr("disabled")){
                   return;
               }
               time();
               var mobile = $('.phone_input').val();
                            
               var verifyCodeData={};
               verifyCodeData['aliSessionId']=aliSessionId;
               verifyCodeData['aliToken']=aliToken;
               verifyCodeData['aliSig']=aliSig;
               verifyCodeData['aliScene']=aliScene;
               verifyCodeData['mobile']=mobile;
             
               
               $.ajax({
            	   type: "POST",
            	   url:"/user/popUserSecurity/getVerifyCode.json",
            	   data:{"verifyCodeData":JSON.stringify(verifyCodeData)},
            	   headers:{
       		        "ip":returnCitySN["cip"]
       		      },
	       		   success: function(data){
	       			if(data.code == '1'){
	                       if(state){
	                           time();
	                       }
	                       state = false;
	                   }else{
	                  	 layer.alert(data.message, {icon: 2});
	                   }
	              	
	                   window.ic.reset();
	                   aliVerifyStatus=false;
	       		  }
            	   });
                           
           });

      },
       initSubmit:function(){
		   //上传图片
		   $.files_up_oss({
				id:"sellerImage_input",//按钮id
		        url: "/oss/getSignatureNew",
		        dir: "subSeller/sellerImage/",
		        file_type: "jpg,png,gif,psd,ai,cdr,eps,ppt,word,excel,pdf,tiff,rar,zip,7z,gif",
		        size: "100mb",//mb
		        view_url: "https://ziyunoss.oss-cn-hangzhou.aliyuncs.com/"					
			},function(index_number,json){										 
				 if (json.Status == 'OK') {
					 $("#sellerImage_input").hide();
					 $(".sellerImage_span").append('<div class="img_obj"><p><img class="up_img" data-type="excel" data-item="'+ json.filename +'" src="https://ziyunoss.oss-cn-hangzhou.aliyuncs.com/' + json.filename+'"><span class="img_del" style="cursor:pointer;">删除</span></p></div>');
					 
				  // $('#sellerImageUrl').attr('src', 'https://ziyunoss.oss-cn-hangzhou.aliyuncs.com/' + json.filename);				   			 
					}
		      });
    	   
		   $(document).on('click','.img_del',function(){		
				  $(this).parents(".img_obj").remove();
				  $("#sellerImage_input").show();
			 });		 
		   
    	   $(document).on('click','.submit_button',function(){ 
    		   var name = $(".contacts_name").val();
    		   var sellerMobile = $(".phone_input").val(); 
    		   var verifyCode = $(".code_input").val(); 
    		   var sellerTel = $(".fixedPhone_input").val(); 
    		   var sellerQq = $(".qq").val(); 
    		   var sellerEmail = $(".email").val(); 
    		   var doorheadName = $(".doorheadNname").val(); 
    		   var sellerName = $(".sub_sellerName").val(); 
    		   var areaId = $(".input_addr").val(); 
    		   var addr = $(".addr_input").val();    		  
    		   var businessUrl = $("#busi_img").attr("data-item");//营业执照 
    		   var sellerType = $(".type_input").val(); 
    		   var monthlyTradingScale = $(".monthlyTradingScale").val(); 
    		   var sellerImageUrl=$(".up_img").attr("data-item");//门头照
    		   
    		   if(sellerType == "供应商"){
    			   sellerType = "0";
    		   }
    		   if(sellerType == "工厂"){
    			   sellerType = "1";
    		   }
    		   
    		   		
    		   if(!sellerName){
                	 layer.alert("请填写子公司名", {icon: 2});
                	 return false;
                 }
    		   if(!areaId){
              	 layer.alert("请填写子公司地址", {icon: 2});
              	 return false;
               }
    		   if(!sellerName){
              	 layer.alert("请填写子公司详细地址", {icon: 2});
              	 return false;
               }
    		   if(!sellerImageUrl){
                	 layer.alert("请上传门头照", {icon: 2});
                	 return false;
                 }
    		   
    		   
    		   var subSellerData={
    				   "name":name,
    				   "sellerMobile":sellerMobile,				   
					   "sellerTel":sellerTel,
					   "sellerQq":sellerQq,
					   "sellerEmail":sellerEmail,
					   "doorheadName":doorheadName,
					   "sellerName":sellerName,
					   "areaId":areaId,
					   "addr":addr,
					   "sellerType":sellerType,
					   "monthlyTradingScale":monthlyTradingScale,
					   "sellerImageUrl":sellerImageUrl,
					   "businessUrl":businessUrl
    		   }
					   
    		    doAjax('/user/subMerchant/addSubMerchant.json','post',{"subSellerData":JSON.stringify(subSellerData),"verifyCode":verifyCode},function(res){
    		    	if (res.code != "1") {
    		    		layer.alert(res.message, {icon: 2});
    		    		return false;
    		    		
					}
    		    	window.location.href="/user/subMerchant/index.do";
    		    	
    		    });
    	   });
       }
      
        
   }
     exports('addSubMerchant',view);
    });